package com.chenzouquan.util.format;

import java.io.*;

public class JsonFileProcessor {
    public static void removeQuotesFromJsonFile(String jsonFilePath) {
        try {
            // 读取 JSON 文件
            BufferedReader reader = new BufferedReader(new FileReader(jsonFilePath));

            // 创建临时文件
            String tempFilePath = jsonFilePath + ".tmp";
            BufferedWriter writer = new BufferedWriter(new FileWriter(tempFilePath));

            String line;
            while ((line = reader.readLine()) != null) {
                // 删除双引号
                String processedLine = line.replace("\"\\", "");
                processedLine = processedLine.replace("\\\"", "");
                // 写入临时文件
                writer.write(processedLine);
                writer.newLine();
            }

            // 关闭读写流
            reader.close();
            writer.close();

            // 删除原始文件
            boolean deleteResult = new File(jsonFilePath).delete();
            if (!deleteResult) {
                System.out.println("无法删除原始文件: " + jsonFilePath);
                return;
            }

            // 重命名临时文件为原始文件名
            boolean renameResult = new File(tempFilePath).renameTo(new File(jsonFilePath));
            if (!renameResult) {
                System.out.println("无法重命名临时文件为原始文件名");
            }

            System.out.println("JSON 文件成功删除双引号");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

